package com.yztc.yidoutang.activity.quanwu_activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.yztc.yidoutang.R;
import com.yztc.yidoutang.activity.qwj_detail_activity.QuanWuDetailActivity;
import com.yztc.yidoutang.adapter.QuanWuAdapter;
import com.yztc.yidoutang.base.BaseActivity;
import com.yztc.yidoutang.bean.QuanWuListBean;

import java.util.List;

/**
 * Created by My on 2016/10/18.
 */

public class QuanWuActivity extends BaseActivity implements QuanWuContract.View{
    private int i=1;
    private int lastItem;
    private SwipeRefreshLayout swipeRefreshLayout;
    private ListView lv;
    private QuanWuAdapter adapter;
    private QuanWuPresenter presenter=new QuanWuPresenter(this);
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        refresh();
    }

    private void refresh() {
        //设置刷新时动画的颜色
        swipeRefreshLayout.setColorSchemeResources(
                R.color.clr_orange);
        //表示刷新时出发的监听事件
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        presenter.getDataForNet(1+"");
                        swipeRefreshLayout.setRefreshing(false);

                    }
                },2000);
            }
        });
        lv.setOnScrollListener(new AbsListView.OnScrollListener() {

        @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                if (scrollState==ListView.OnScrollListener.SCROLL_STATE_IDLE && lastItem==adapter.getCount()){
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            int page=i++;
                            presenter.getDataForNet(page+"");
                        }
                    },2000);
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                lastItem=totalItemCount;
            }
        });
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_quanwuji;
    }

    private void initView() {
        lv= (ListView) findViewById(R.id.qwj_lv);
        swipeRefreshLayout= (SwipeRefreshLayout) findViewById(R.id.qwj_swipe_layout);
        presenter.getDataForNet(1+"");
    }


    @Override
    public void onQuanWuSuccess(final List<QuanWuListBean> quanWuListBeanList) {
        if (adapter==null){
            adapter=new QuanWuAdapter(QuanWuActivity.this,quanWuListBeanList);
            lv.setAdapter(adapter);
        }else {
            adapter.notifyDataSetChanged();
        }

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent=new Intent(QuanWuActivity.this, QuanWuDetailActivity.class);
                intent.putExtra("id",quanWuListBeanList.get(position).getGroupId());
                startActivity(intent);
            }
        });


    }

    @Override
    public void onQuanWuFail(String msg) {
        Toast.makeText(QuanWuActivity.this,"获取数据失败",Toast.LENGTH_SHORT).show();
    }
    public void QWClick(View view){
        switch (view.getId()){
            case R.id.qwj_btn_back:
                finish();
                break;
            case R.id.qwj_btn_shaixuan:
                break;
        }
    }
}
